package com.lovo.util;


import com.lovo.util.MQUtil;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

/**
 * 交换机队列，账号注册
 */
@Service
public class PscRabiitmq {
    //注入工厂（已经预加载）
    @Autowired
    CachingConnectionFactory connectionFactory;

    //rabbitmq模板
    @Bean(value = "rabbitTemplate")
    public RabbitTemplate rabbitTemplate() {
        return new RabbitTemplate(connectionFactory);
    }

    //队列
    @Bean
    public Queue requestQueue() {
        return new Queue(MQUtil.REQUESTQUEUE);
    }

    //交换机
    @Bean
    public DirectExchange directExchangeOrder() {
        return new DirectExchange(MQUtil.DIRECTEXCHANGEREQUEST);
    }

    //绑定队列和交换机
    @Bean
    public Binding directExchangeOrderToOrderQueue(Queue orderQueue, DirectExchange directExchangeOrder) {
        //队列绑定交换机设置key
        return BindingBuilder.bind(orderQueue).to(directExchangeOrder).with(MQUtil.REQUESTKEY);
    }
}
